package com.yzh.wanandroid.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.yzh.wanandroid.R
import com.yzh.wanandroid.databinding.ItemIntegralBinding
import com.yzh.wanandroid.databinding.ItemIntegralHeadBinding
import com.yzh.wanandroid.network.retrofit.responsebean.IntegralData

class IntegralListAdapter : PagingDataAdapter<IntegralData, RecyclerView.ViewHolder>(
    ARTICLE_DIFF_CALLBACK
) {
    /** 头部 */
    private val HEAD_ITEM = 0

    /** 列表 */
    private val LIST_ITEM = 1

    var mIntegral = "----"

    fun setHeadData(integral: String) {
        this.mIntegral = integral
        this.notifyDataSetChanged()
    }

    override fun getItemViewType(position: Int): Int {
        if (position == 0) {
            return HEAD_ITEM
        }
        return LIST_ITEM;
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
       if (getItemViewType(position) == HEAD_ITEM) {
           if (holder is HeadItemHolder) {
               holder.bind()
           }
       } else {
           if (holder is ListItemHolder) {
               holder.bind(getItem(position))
           }
       }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == HEAD_ITEM) {
            val headbinding: ItemIntegralHeadBinding =
                DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_integral_head,
                    parent,
                    false
                )
            return HeadItemHolder(headbinding)
        } else {
            val itemList: ItemIntegralBinding =
                DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_integral,
                    parent,
                    false
                )
            return ListItemHolder(itemList)
        }
    }


    inner class HeadItemHolder(private val binding: ItemIntegralHeadBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind() {
            binding.run {
                adapter = this@IntegralListAdapter
                executePendingBindings()
            }
        }
    }

    inner class ListItemHolder(private val binding: ItemIntegralBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(data: IntegralData?) {
            binding.run {
                model = data
                executePendingBindings()
            }
        }
    }

    companion object {
        private val ARTICLE_DIFF_CALLBACK: DiffUtil.ItemCallback<IntegralData> =
            object : DiffUtil.ItemCallback<IntegralData>() {
                override fun areItemsTheSame(
                    oldResult: IntegralData,
                    newResult: IntegralData
                ): Boolean {
                    return oldResult.id == newResult.id
                }

                @SuppressLint("DiffUtilEquals")
                override fun areContentsTheSame(
                    oldResult: IntegralData,
                    newResult: IntegralData
                ): Boolean {
                    return oldResult == newResult
                }
            }
    }
}